﻿Imports System
Imports System.Runtime.InteropServices    '  SizeOf
Imports System.Text                       '  StringBuilder


Namespace GCS_Tools
    Public Class DeviceID

        <DllImport("CoreDLL.DLL")> _
        Private Shared Function GetDeviceUniqueID( _
        ByVal sApp() As Byte, ByVal lAppLen As Integer, ByVal DV As Integer, ByVal IDOut() As Byte, ByRef IDLen As Integer) As Integer

        End Function

        Public Shared Function GetDeviceID(ByVal sAppName As String) As String
            Dim btApp() As Byte             'btApp至少8个字节
            If sAppName.Length < 8 Then ReDim btApp(7) Else ReDim btApp(sAppName.Length - 1)

            For I As Integer = 0 To sAppName.Length - 1
                Dim Int1 As Integer = AscW(Mid(sAppName, I + 1, 1))
                Dim DivideBy As Integer = 256
                btApp(I) = CByte(Int1 Mod DivideBy)
            Next
            If sAppName.Length < 8 Then
                For I As Integer = sAppName.Length To 7
                    btApp(I) = Byte.MinValue
                Next
            End If

            Dim lAppLen As Integer = btApp.Length
            Dim IDout(19) As Byte
            Dim IDLen As Integer = IDout.Length

            Dim ret As Integer
            ret = GetDeviceUniqueID(btApp, lAppLen, 1, IDout, IDLen)

            Dim sLine As String = ""
            For I As Int16 = 0 To 3
                For J As Int16 = 0 To 4
                    sLine &= String.Format("{0,3},", IDout(I * 5 + J))
                Next
                sLine &= vbCrLf
            Next
            Return sLine
        End Function

        Public Shared Function WriteDiviceID(ByVal sAppName As String) As Boolean
            Dim str As String = GetDeviceID(sAppName)
            Dim GE As New lvlRegistry
            Dim strKeyName As String = "注册码"
            Dim strValueName As String = sAppName

            If GE.SetValue(strKeyName, strValueName, str) Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Shared Function ExamDeviceID(ByVal sAppName As String) As Boolean
            Dim str As String = GetDeviceID(sAppName)

            Dim GE As New lvlRegistry
            Dim strKeyName As String = "注册码"
            Dim strValueName As String = sAppName
            Dim ProID As String = ""
            If GE.GetValue(strKeyName, strValueName, ProID) Then
                If str.CompareTo(ProID) = 0 Then
                    Return True
                End If
            End If
            Return False
        End Function


    End Class
End Namespace

